package com.yvon.maple.service.auth.handler;

import com.yvon.boot.common.result.ResponseData;
import com.yvon.boot.common.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.*;

/**
 * @author : Yvon
 * @since : 2021-08-23
 */
@ControllerAdvice
@Slf4j
public class AuthenticationExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = AuthenticationException.class)
    public ResponseData<?> authenticationException(AuthenticationException e) {
        log.error("登录异常：ex={}", e.getMessage(), e);
        if ("Bad credentials".equals(e.getMessage())) {
            return ResponseData.failed(ResultCode.JWT_USER_INVALID);
        }
        return ResponseData.failed(ResultCode.JWT_USER_INVALID.getCode(),e.getMessage());
    }


}
